//
//  DeviceProfileViewController.m
//  EZ Tracker
//
//  Created by FENGYL on 2017/3/16.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

#import "DeviceProfileViewController.h"
#import "PullUpMenuView.h"
#import "UIView+XL.h"
#import "NetworkCommand.h"
#import "UserModel.h"
#import "NavigatingViewController.h"

#import <SVProgressHUD.h>

@interface DeviceProfileViewController ()

@property (weak, nonatomic) IBOutlet UILabel *deviceNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *serialNumberLabel;
@property (weak, nonatomic) IBOutlet UIScrollView *infomationScrollView;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;

@property(copy, nonatomic) NSString *deviceName;
@property(copy, nonatomic) NSString *serialNumber;
@property(strong, nonatomic)UserModel *userModel;
@property(strong, nonatomic)PullUpMenuView *pullUpMenuView;

@end

@implementation DeviceProfileViewController

-(UserModel *)userModel{
    
    if (!_userModel) {
        _userModel = [[UserModel alloc]init];
    }
    return _userModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置deviceName
    if ([self.userModel.changeDeviceNameDictionary valueForKey:self.serialNumber]) {
        
        [self.deviceNameLabel setText:[self.userModel.changeDeviceNameDictionary valueForKey:self.serialNumber]];
        
    }else{
        
        [self.deviceNameLabel setText:self.deviceName];
        
    }
    
//    [self.deviceNameLabel setText:self.deviceName];
    [self.serialNumberLabel setText:self.serialNumber];
    
//    UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(20, 30, 100, 100)];
//    
//    [redView setBackgroundColor:[UIColor redColor]];
//    
//    [self.infomationScrollView addSubview:redView];
    
    self.pullUpMenuView = [[[NSBundle mainBundle]loadNibNamed:@"PullUpMenuView" owner:nil options:nil] firstObject];
    
    
    [self.pullUpMenuView setUpPullUpView];
    
    self.pullUpMenuView.width = [UIScreen mainScreen].bounds.size.width;
    
//    pullUpMenuView.height = [UIScreen mainScreen].bounds.size.height - [UIApplication sharedApplication].statusBarFrame.size.height;

    [self.pullUpMenuView hidePullUpView];
    
    self.infomationScrollView.contentSize = CGSizeMake(0, self.pullUpMenuView.height);
    
    [self.infomationScrollView addSubview:self.pullUpMenuView];
    
    
    [self getInfo];
    
    
    
    
    
}

#pragma mark 获取信息
-(void)getInfo{
    
    [SVProgressHUD showWithStatus:@"Loading..."];
    
    [NetworkCommand getLastStatusDataForSerialNumber:self.serialNumber Success:^(NSArray *responseObject) {
        
        
        
        NSArray *responseObject1 = responseObject;
        
        
            
        if ([[responseObject1 firstObject] class] == [NSNull class]) {
            
            [SVProgressHUD showErrorWithStatus:@"No Data."];
            
            [SVProgressHUD dismissWithDelay:1];
            
            return;
                
        }
            
            
            
        
        
        
        [NetworkCommand getLastTrackPoiDataBySnsForSerialNumber:self.serialNumber Success:^(NSArray *responseObject) {
            
            
                
//            NSLog(@"%@",[responseObject firstObject]);
            
            if ([[responseObject firstObject] class] == [NSNull class]) {
                    
                [SVProgressHUD showErrorWithStatus:@"No Data."];
                
                [SVProgressHUD dismissWithDelay:1];
                    
                return;
                    
            }
                
            
            
            [SVProgressHUD dismiss];
            
            [self.pullUpMenuView setUpPullUpViewForTrackPoiDataResponseObject:responseObject StatusDataResponseObject:responseObject1];
            
//            self.pullUpMenuView red
            
        } failure:^{
            
            [SVProgressHUD showErrorWithStatus:@"No Data."];
            
            [SVProgressHUD dismissWithDelay:1];
            
        }];
        
    } failure:^{
        
        [SVProgressHUD showErrorWithStatus:@"No Data."];
        
        [SVProgressHUD dismissWithDelay:1];
        
    }];
    
}

#pragma mark 初始化设置
-(void)setUpInfoForSerialNumber:(NSString *)serialNumber DeviceName:(NSString *)deviceName{
    
    self.serialNumber = serialNumber;
    self.deviceName = deviceName;
    
}

#pragma mark 设置头像
-(void)setUpIconForImage:(UIImage *)image{
    
    if (!image) {
        
        image = [UIImage imageNamed:@"ic_tracker_useractivity_yuan_2"];
        
    }
    
    
    [self.iconImageView setImage:image];
    
}

#pragma mark TrackNow Button
- (IBAction)touchTrackNowButton:(id)sender {
    
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"NavigatingViewController" bundle:[NSBundle mainBundle]];
    
    NavigatingViewController *navigatingViewController = [storyBoard instantiateViewControllerWithIdentifier:@"NavigatingViewController"];
    
    navigatingViewController.selectedSerialNumber = self.serialNumber;
    
    navigatingViewController.isSetting = YES;
    
    [self.parentViewController showViewController:navigatingViewController sender:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
